package com.wizo.csb.storages;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;

import com.wizo.csb.Storage;

public class AmazonS3AWS extends Storage{

	public static final String VERBOSE = "amazonS3.verbose";
	public static final String VERBOSE_DEFAULT = "true";
	private boolean _verbose;
	
	public static final String AWSCREDENTIALS = "awsCredentials";
	
	private AmazonS3 _s3;
	
	public AmazonS3AWS(){
		
	}
	
	public void init(){
		_verbose = Boolean.parseBoolean(getProperties().getProperty(VERBOSE, VERBOSE_DEFAULT));

		ClientConfiguration conf = new ClientConfiguration();
		
		String proxyHost = getProperties().getProperty("proxyHost", "");
		String proxyPort = getProperties().getProperty("proxyPort", "");
		if((!proxyHost.equals(""))&&(!proxyPort.equals(""))){
			conf.setProxyHost(proxyHost);
			conf.setProxyPort(Integer.parseInt(proxyPort));
		}
		
		try{
			_s3 = new AmazonS3Client(new PropertiesCredentials(
					new FileInputStream(getProperties().getProperty(AWSCREDENTIALS,
					"AwsCredentials.properties"))), conf);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}

	@Override
	public InputStream download(String bucket, String name) {
		S3Object s3Object;
		InputStream contents = null;

		s3Object = _s3.getObject(new GetObjectRequest(bucket, name));
		contents = s3Object.getObjectContent();
		
		if(_verbose&&(contents!=null)){
			System.out.println("Download successfully! object:"+name);
		}
		
		return contents;
	}

	@Override
	public int upload(String bucket, String name, InputStream inputStream,
			long contentLength, String contentType) {
		ObjectMetadata metadata = new ObjectMetadata();
		metadata.setContentLength(contentLength);
		metadata.setContentType(contentType);
		
		_s3.putObject(bucket, name, inputStream, metadata);
		
		if(_verbose){
			System.out.println("Upload successfully! object:"+name);
		}
		return 0;
	}

	@Override
	public int delete(String bucket, String name) {
		_s3.deleteObject(bucket, name);

		if(_verbose){
			System.out.println("Delete successfully! object:"+name);
		}
		return 0;
	}

	@Override
	public int copy(String bucketS, String objectS, String bucketD,
			String objectD, boolean replaceMetadata) {
		_s3.copyObject(bucketS, objectS, bucketD, objectD);

		if(_verbose){
			System.out.println("Copy successfully! object:"+objectS);
		}
		return 0;
	}

	@Override
	public int move(String bucketS, String objectS, String bucketD,
			String objectD, boolean replaceMetadata) {
		_s3.copyObject(bucketS, objectS, bucketD, objectD);
		_s3.deleteObject(bucketS, objectS);
		
		if(_verbose){
			System.out.println("Move successfully! object:"+objectS);
		}
		return 0;
	}
	
	
}
